package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentServicesMessageBinding;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.view.ui.adapter.CommPagerAdapter6;
import com.sgb.kjwl.viewmodel.enterprise.ServiceForMessageViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

/**
 * 企业团队网站，经营业务企业经营，企服需求
 */
public class ServiceForMessageFragment extends AppLazyFragment {

    private FragmentServicesMessageBinding mBinding;
    private ServiceForMessageViewModel mViewModel;
    private CommPagerAdapter6 pagerAdapter;




    public static ServiceForMessageFragment newInstance(String websiteNo, String websiteType) {
        ServiceForMessageFragment fragment = new ServiceForMessageFragment();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putString("websiteType", websiteType + "");
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_services_message,container,false);
        mViewModel = new ViewModelProvider(this).get(ServiceForMessageViewModel.class);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initObsever();

    }


    private void initObsever(){

        mViewModel.websiteNo = getArguments().getString("websiteNo");
        mViewModel.websiteType = getArguments().getString("websiteType");
        mViewModel.showToast.observe(ServiceForMessageFragment.this.getActivity(),String ->{
            MToast.showLongToast(ServiceForMessageFragment.this.getContext(),String);
        });

        mViewModel.show_loading.observe(ServiceForMessageFragment.this.getActivity(),inttype ->{
           switch (inttype){
               case 1:
                   DialogHelper.showProgressDialog(ServiceForMessageFragment.this.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                   break;
               case 2:
                   DialogHelper.dismissProgressDialog();
                   break;
           }
        });

        mViewModel.listdata.observe(ServiceForMessageFragment.this.getActivity(),list ->{
            ParentAreaEntity data = new ParentAreaEntity();
            data.setName("全部");
            data.setId("");
            mViewModel.mFragments.clear();
            list.add(0,data);
            for (int i = 0; i < list.size(); i++) {
                    mViewModel.mFragments.add(ProfessionalQuoteFragment.newInstance(list.get(i).getChildren(), list.get(i).getId(),mViewModel.websiteNo,mViewModel.websiteType));
            }
            pagerAdapter = new CommPagerAdapter6(getChildFragmentManager(),  mViewModel.mFragments, list);
            //mBinding.viewpager.setOffscreenPageLimit(list.size());
            mBinding.viewpager.setAdapter(pagerAdapter);
            mBinding.tabLayout.setupWithViewPager(mBinding.viewpager);
            mBinding.tabLayout.getTabAt(0).select();
        });
        mViewModel.getServiceType();
    }

    @Override
    protected void onLazyLoad() {

    }
}
